//
//  XJWalletChargeViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/23.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  钱包充值

#import "XJWalletChargeViewController.h"
#import "XJWebViewController.h"

//View
#import "XJWalletSectionHeadView.h"
#import "XJWalletChargeCell.h"
#import "XJWalletChargeFooterView.h"
#import "XJWalletChargeBottomView.h"

//model
#import "XJWalletChargeModel.h"



@interface XJWalletChargeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, strong) UITableView *tableView;

@property(nonatomic, copy) NSArray *sectionArr;

/**数据源*/
@property(nonatomic, strong) NSMutableArray *rowDataArr;

@property(nonatomic, strong) XJWalletChargeFooterView *tableVieFooterView;
@property(nonatomic, strong) XJWalletChargeBottomView *bottomView;

@end

@implementation XJWalletChargeViewController

#pragma mark - 一 lazy
- (NSMutableArray *)rowDataArr {
    if (!_rowDataArr) {
        _rowDataArr = [NSMutableArray array];
    }
    return _rowDataArr;
}

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navTitle = @"充值";
    
    [self setupView];

    [self configureData];
}

///**1.icon*/
//@property (nonatomic, copy) NSString *iconStr;
///**2.充值方式(支付宝,微信)*/
//@property (nonatomic, copy) NSString *name;
///**3.是否选中*/
//@property (nonatomic, assign) BOOL isChecked;

- (void)configureData {
    self.sectionArr = @[@"请输入充值金额",@"请选择付款方式"];

    NSArray *tempArr = @[@{@"iconStr":@"pay_alipay",
                           @"name":@"支付包支付",
                           @"isChecked":@0
                           },
                         @{@"iconStr":@"pay_WeChat",
                           @"name":@"微信支付",
                           @"isChecked":@0
                           }
                         ];
    
    for (NSDictionary *dic in tempArr) {
        XJWalletChargeModel *model = [XJWalletChargeModel mj_objectWithKeyValues:dic];
        [self.rowDataArr addObject:model];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
}

- (void)setupView {
    self.tableView.frame = CGRectMake(0, 0, KWidth, KHeight - XJTopNavH - 50);
    [self.view addSubview:self.tableView];
    
    self.tableView.tableFooterView = self.tableVieFooterView;
    
    self.bottomView.frame = CGRectMake(0, CGRectGetMaxY(self.tableView.frame) , KWidth, 50);
    [self.view addSubview:self.bottomView];
}
#pragma mark - 二 lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:(CGRectZero) style:(UITableViewStylePlain)];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        
        _tableView.sectionHeaderHeight = 40;
        
        //注册
        [_tableView registerClass:[XJWalletSectionHeadView class] forHeaderFooterViewReuseIdentifier:@"XJWalletSectionHeadViewId"];
        [_tableView registerClass:[XJWalletChargeInputMoneyCell class] forCellReuseIdentifier:@"XJWalletChargeInputMoneyCellId"];
        [_tableView registerClass:[XJWalletChargeCell class] forCellReuseIdentifier:@"XJWalletChargeCellId"];
    }
    return _tableView;
}
- (XJWalletChargeFooterView *)tableVieFooterView {
    if (!_tableVieFooterView) {
        _tableVieFooterView = [[XJWalletChargeFooterView alloc] init];
        _tableVieFooterView.frame = CGRectMake(0, 0, KWidth, 50);
        
        _tableVieFooterView.clickChargeProtocalBlock = ^{
            XJLog(@"点击了充值协议");
//            [JRToast showWithText:@"点击充值协议"];
            
            XJWebViewController *webVc = [[XJWebViewController alloc] init];
            webVc.vcType = XJWebVCTypeChargeProtocol;
            [self.navigationController pushViewController:webVc animated:YES];
        };
    }
    return _tableVieFooterView;
}

- (XJWalletChargeBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[XJWalletChargeBottomView alloc] init];
    }
    return _bottomView;
}


#pragma mark - 三 UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sectionArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else {
        return self.rowDataArr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) { // XJWalletChargeInputMoneyCell
        XJWalletChargeInputMoneyCell *inputMoneyCell = [tableView dequeueReusableCellWithIdentifier:@"XJWalletChargeInputMoneyCellId"];
        inputMoneyCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        inputMoneyCell.walletChargeInputMoneyCellBlock = ^(NSString *money) {
            XJLog(@"输入金额是:%@",money);
        };
        
        return inputMoneyCell;
        
    }else {
        XJWalletChargeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XJWalletChargeCellId"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        //取出模型
        XJWalletChargeModel *currentModel = self.rowDataArr[indexPath.row];
        
        //给cell赋值
        [cell configureWalletChargeCellWithIcon:currentModel.iconStr payTypeStr:currentModel.name isSelected:currentModel.isChecked];
        
        return cell;
    }
}
#pragma mark - UITableViewDelegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    XJWalletSectionHeadView *sectionView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"XJWalletSectionHeadViewId"];
    
    [sectionView configureWalletSectionHeadViewWithName:self.sectionArr[section]];
    
    return sectionView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 100;
    }else {
        return 44;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        
        //全部取消选中
        for (XJWalletChargeModel *model in self.rowDataArr) {
            model.isChecked = NO;
        }
        
        //当前model选中状态
        XJWalletChargeModel *model = self.rowDataArr[indexPath.row];
        model.isChecked = !model.isChecked;
        [self.tableView reloadData];
        XJLog(@"%@",model.name);
    }
}

#pragma mark - 回收键盘

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
